package com.xli.mis.datasource.controller;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xli.dto.component.model.TableModel;
import com.xli.dto.controller.BaseController;
import com.xli.dto.params.IdsDTO;
import com.xli.dto.result.ResultVO;
import com.xli.dto.result.status.Status;
import com.xli.dto.validation.group.IGroup;
import com.xli.mis.datasource.entity.DataSource;
import com.xli.mis.datasource.entity.dto.DataSourceAddDTO;
import com.xli.mis.datasource.entity.dto.DataSourceSearchDTO;
import com.xli.mis.datasource.entity.dto.DataSourceUpdateDTO;
import com.xli.mis.datasource.entity.mapper.IDataSourceMapper;
import com.xli.mis.datasource.entity.vo.DataSourceVO;
import com.xli.mis.datasource.service.IDataSourceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@Tag(name = "数据源管理")
@RestController
@RequestMapping("/dataSource")
public class DataSourceController extends BaseController {

    @Autowired
    IDataSourceService iDataSourceService;

    @Operation(summary = "保存数据源", description = "保存数据源")
    @PostMapping(value = "/add")
    public ResultVO<String> add(@RequestBody @Validated(IGroup.add.class) DataSourceAddDTO dto) {
        DataSource dataSource = iDataSourceService.findByName(dto.getDsName());
        if (dataSource == null) {
            dataSource = IDataSourceMapper.INSTANCE.toEntity(dto);
            dataSource.setId(IdUtil.getSnowflakeNextId());
            if (iDataSourceService.insert(dataSource)) {
                return new ResultVO<>(Status.SUCCESS, "保存成功");
            }
            return new ResultVO<>(Status.FAILED, "保存失败");
        }
        return new ResultVO<>(Status.FAILED, "保存失败，数据源名称已存在");
    }

    @Operation(summary = "删除数据源", description = "删除数据源")
    @PostMapping(value = "/delete")
    public ResultVO<String> delete(@RequestBody @Validated(IGroup.delete.class) IdsDTO dto) {
        int i = 0;
        for (String id : dto.getIds()) {
            if (iDataSourceService.delete(Long.valueOf(id))) {
                i++;
            }
        }
        return new ResultVO<>(Status.SUCCESS, i + "条数据删除成功");
    }

    @Operation(summary = "更新数据源", description = "更新数据源")
    @PostMapping(value = "/update")
    public ResultVO<String> update(@RequestBody @Validated(IGroup.update.class) DataSourceUpdateDTO dto) {
        DataSource dataSource = iDataSourceService.find(Long.valueOf(dto.getId()));
        if (dataSource != null) {
            if (iDataSourceService.update(IDataSourceMapper.INSTANCE.toEntity(dto))) {
                return new ResultVO<>(Status.SUCCESS, "更新成功");
            }
            return new ResultVO<>(Status.FAILED, "更新失败");
        } else {
            return new ResultVO<>(Status.FAILED, "更新失败，数据不存在");
        }
    }

    @Operation(summary = "查询数据源列表", description = "查询数据源列表")
    @PostMapping(value = "/search")
    public ResultVO<TableModel<DataSourceVO, DataSource, DataSourceSearchDTO>> search(@RequestBody @Validated(IGroup.search.class) DataSourceSearchDTO dto) {

        TableModel<DataSourceVO, DataSource, DataSourceSearchDTO> tableModel = new TableModel<>(dto) {
            @Override
            public List<DataSourceVO> fetch(int current, int pageSize) {
                Page<DataSource> page = iDataSourceService.findList(getQueryWrapper(), current, pageSize);
                this.setTotal(page.getTotal());
                return page.convert(IDataSourceMapper.INSTANCE::toVO).getRecords();
            }
        };
        return new ResultVO<>(Status.SUCCESS, "查询成功", tableModel);
    }
}
